//
//  DTagHistoriesViewController.swift
//  Drop
//
//  Created by hushaohua on 2017/12/20.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class DTagHistoryTableViewCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class DTagHistoriesViewController: UITableViewController {

    var tag:Tag?{
        didSet{
            if (tag != nil){
                self.histories = tag!.histories!.sortedArray(using: [TagHistory.defaultSort()]) as? [TagHistory];
                self.title = tag!.name;
            }else{
                self.histories = nil;
                self.title = "";
            }
        }
    }
    var histories:[TagHistory]?;
    override func viewDidLoad() {
        super.viewDidLoad()

        self.configTableView();
        if (self.tag != nil){
            self.title = self.tag!.name;
        }
    }
    
    func configTableView() -> Void{
        self.tableView.tableFooterView = UIView();
        self.tableView.rowHeight = 44;
        self.tableView.register(DTagHistoryTableViewCell.self, forCellReuseIdentifier: "cell");
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.histories != nil){
            return self.histories!.count + 1;
        }else{
            return 0;
        }
    }

    lazy var dateFormat:DateFormatter = {
        var formatter:DateFormatter = DateFormatter();
        formatter.dateFormat = "yyyy-MM-dd HH:mm";
        return formatter;
    }();
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if (indexPath.row == 0){
            cell.textLabel?.text = self.tag?.name;
            cell.detailTextLabel?.text = self.dateFormat.string(from: self.tag!.lastUpdatedDate! as Date);
        }else{
            let history:TagHistory = self.histories![indexPath.row - 1];
            cell.textLabel?.text = history.name;
            if (history.created != nil){
                cell.detailTextLabel?.text = self.dateFormat.string(from: history.created! as Date);
            }else{
                cell.detailTextLabel?.text = "";
            }
        }
        
        return cell
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
